﻿#region Copyrigth and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ucenter
 * ==============================================================================*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;
using System.IO;
using UCenter.Core.Common;

namespace UCenter.ContactService.Email {
    public class EmailSohuService : IEmailService {
        #region Fields

        #endregion

        #region Constructors

        #endregion

        #region Properties
        public string EmailSystem {
            get { return "sohu.com"; }
        }

        public string StartUrl {
            get { return "http://passport.sohu.com/sso/login.jsp?"; }
        }

        public Encoding DefaultEncoding {
            get { return Encoding.GetEncoding("gb2312"); }
        }
        #endregion

        #region Events

        #endregion


        public MailContractCollection GetContracts(string userName, string pwd) {
            MailContractCollection mc = new MailContractCollection();
            string returnUrl = StartUrl;
            CookieContainer cc = new CookieContainer();
            string q = StartUrl + string.Format("userid={0}%40sohu.com&password={1}&appid=1000&persistentcookie=0&s=1222320469996&b=1&w=1280&pwdtype=1", userName, System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "MD5").ToLower());
            string accept = "image/gif, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-silverlight-2-b2, application/x-silverlight, */*";
            string language = "zh-cn";
            string refer = "http://mail.sohu.com/";
            string contentType = "application/x-www-form-urlencoded";
            string startPageHTMl = WebOpHelper.GetHtml(q, accept, contentType, refer,
                language, cc, Encoding.GetEncoding("gb2312"), out returnUrl);
            if (startPageHTMl.IndexOf("success") > -1) {
                string url = "http://mail.sohu.com/bapp/50/contact";
                string html = WebOpHelper.GetHtml(url, accept, contentType, refer, language, cc, Encoding.GetEncoding("gb2312"), out returnUrl);
                var o = JsonFormaterUtils.Deserialize<SohuJson>(html, Encoding.GetEncoding("gb2312"));
                if (o != null && o.status == 0) {
                    foreach (var n in o.listString) {
                        MailContract m = new MailContract();
                        m.Email = n.email;
                        m.Name = n.name;
                        if (string.IsNullOrEmpty(m.Name)) {
                            m.Name = m.Email;
                        }
                        mc.Add(m);
                    }
                }
                
            }
            return mc;

        }
    }
}
